package com.devsoft.foodfrenzy;
//activity started when opening the application

import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.database.sqlite.SQLiteDatabase;
import android.view.KeyEvent;

public class GameActivity extends BaseGameActivity
{
	private Camera camera;
	private ResourcesManager resourcesManager;
	//ScoreManager myDB = new ScoreManager(this);

	
    public EngineOptions onCreateEngineOptions()
    {
    	camera = new Camera(0, 0, 800, 480);	//sets camera palette to 800 x 480 (most frequently used)
    	
    	//next line forces landscape mode and allows resize for different screen resolutions
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(800, 480), this.camera);
        engineOptions.getAudioOptions().setNeedsMusic(true);
        engineOptions.getAudioOptions().setNeedsSound(true);	//allows for music and sound
        engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        
        return engineOptions;
        
    }
    

    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
    {
    	ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
        resourcesManager = ResourcesManager.getInstance();
        pOnCreateResourcesCallback.onCreateResourcesFinished();
        
    }
    

    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
    {
    	SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
        
    }
    

    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
    {
    	mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback()
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                SceneManager.getInstance().createTitleScene();
                
            }
            
        }));
    	
        pOnPopulateSceneCallback.onPopulateSceneFinished();
        
    }
    
    public int getHighScore(int levelNum){
        ScoreManager myDB = new ScoreManager(this);
        int myReturn = myDB.readHighScore(levelNum);
        myDB.close();
        return myReturn;
    }
 
    
    @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions)	//limits the FPS to 60
    {
        return new LimitedFPSEngine(pEngineOptions, 60);
        
    }
    
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK)
        	SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
        
        return false; 
        
    }
    
    
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    	
        System.exit(0);	
        
    }

}
